<template>
  <el-dialog
    v-model="dialogVisible"
    :title="ruleForm.id ? '编辑' : '增加回款计划'"
    width="30%"
    :before-close="handleClose"
  >
    <div class="class-dialog">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="期次" prop="num" style="width: 100%">
          <el-input placeholder="期次" v-model="ruleForm.num" />
        </el-form-item>
        <el-form-item label="金额（元）" prop="plan_money">
          <el-input placeholder="金额（元）" v-model="ruleForm.plan_money" />
        </el-form-item>
        <el-form-item label="日期" prop="plan_date" style="width: 100%">
          <el-date-picker
            style="width: 100%"
            v-model="ruleForm.plan_date"
            type="date"
            placeholder="日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import {
  receivablesPlanCreateApi,
  receivablesPlanSaveApi,
} from "@/plugins/api/customer/manage/tab6.js";
const props = defineProps({
  treeData: {
    type: Array,
    default: [],
  },
});
let ruleForm = ref({});
let rules = {
  num: [{ required: true, message: "请输入期次", trigger: "blur" }],
  plan_money: [{ required: true, message: "请输入金额", trigger: "blur" }],
  plan_date: [{ required: true, message: "请选择日期", trigger: "change" }],
};
let dialogVisible = ref(false);
function handleClose(done) {
  // dialogVisible.value = false;
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(row, type) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  ruleForm.value = {};
  ruleForm.value.customer_id = row.customer_id;
  ruleForm.value.order_no = row.order_no;
  if (type) {
    ruleForm.value = { ...ruleForm.value, ...row };
  }
  dialogVisible.value = true;
}

const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      let obj = { ...ruleForm.value };
      if (!obj.id) {
        obj.plans = [
          {
            num: ruleForm.value.num,
            plan_money: ruleForm.value.plan_money,
            plan_date: ruleForm.value.plan_date,
          },
        ];
        receivablesPlanCreateApi(obj).then((data) => {
          ElMessage.success("新增成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      } else {
        receivablesPlanSaveApi(obj).then((data) => {
          ElMessage.success("修改成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      }
    }
  });
}

onMounted(() => {});
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss"></style>
